
import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '@/store'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: () => import('@/views/login') },
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: '/dashboard',
    children: [
      { path: '/article', component: () => import('@/views/article') },
      { path: '/dashboard', component: () => import('@/views/dashboard') }

    ]
  }
]

const router = new VueRouter({
  routes
})
// 设置前置路由守卫
router.beforeEach((to, from, next) => {
  // const token = getToken() 这里要在vuex里面获取token
  // console.log(store)
  const token = store.state.user.token
  if (!token && to.path !== '/login') return next('/login')
  next()
})

export default router
